package game;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Screen extends JFrame {
	private JLabel[][] labels = new JLabel[20][20];

	private String[][] level;

	private String[][] rines;

	String path = "./Image/Heartbreak_Ridge.jpg";
	BufferedImage image = null;

	JPanel gameArea;

	public String getRine(int no, String dir) {
		String str = "./Image/marine" + no + "_" + dir.charAt(0) + ".png";
		return str;
	}

	public Screen(String[][] level, int posX, int posY, JPanel panel) {
		super("Awesome Shooter v. 1.0");
		this.level = level;

		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setLocation(100, 100);
		this.setSize(1024, 640);
		this.setResizable(true);
		this.setVisible(true);
		this.setLayout(new BorderLayout());
		this.addWindowListener(new WindowAdapter()  {
			@Override
			public void windowClosed(WindowEvent e) {
				Game.getInstance().setVisible(false);
			}
			
		});
		try {
			image = ImageIO.read(new File(path));
		} catch (IOException e) {
			e.printStackTrace();
		}
		gameArea = new JPanel() {

			protected void paintComponent(Graphics g) {
				super.paintComponent(g);

				g.drawImage(image, 0, 0, this);
			}
		};
		gameArea.setOpaque(true);
		this.getContentPane().add(gameArea, BorderLayout.CENTER);
		GridLayout temp = new GridLayout(20, 20, 0, 0);
		temp.setHgap(0);
		temp.setVgap(0);
		gameArea.setLayout(temp);
		this.getContentPane().add(panel, BorderLayout.EAST);
		draw(posX, posY);
		
		ComponentListener l = new ComponentListener() {
			// This method is called after the component's size changes
			public void componentResized(ComponentEvent evt) {
				Screen c = (Screen) evt.getSource();

				// Get new size
				c.reDraw();
			}

			@Override
			public void componentMoved(ComponentEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void componentShown(ComponentEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void componentHidden(ComponentEvent e) {
				// TODO Auto-generated method stub
				
			}
		};
		
		this.addComponentListener(l);
	}

	public void movePlayerOnScreen(Player p, int x, int y, String playerDirection) {

		// labels[oldX][oldY].setIcon(new ImageIcon("./Image/gulv2.png"));
		labels[p.getXpos()][p.getYpos()].setIcon(null);

		labels[x][y].setIcon(new ImageIcon((Toolkit.getDefaultToolkit().getImage(getRine(p.getColor(), playerDirection)).getScaledInstance(
													(this.getSize().width - 10) / 20,
													(this.getSize().height - 10) / 20,
													Image.SCALE_SMOOTH))));
	}
	
	public void setIcon(Image newIcon, int x, int y) {
		labels[x][y].setIcon(new ImageIcon(newIcon.getScaledInstance(
													(this.getSize().width - 10) / 20,
													(this.getSize().height - 10) / 20,
													Image.SCALE_SMOOTH)));
	}

	public void draw(int posX, int posY) {
		for (int j = 0; j < 20; j++) {
			for (int i = 0; i < 20; i++) {
				if (level[i][j].equalsIgnoreCase("w")) {
					Image test = Toolkit.getDefaultToolkit().getImage("./Image/sten.png");

					JLabel l = new JLabel(new ImageIcon(test.getScaledInstance(
													(this.getSize().width - 10) / 20,
													(this.getSize().height - 10) / 20,
													Image.SCALE_SMOOTH)));
					gameArea.add(l);
					labels[i][j] = l;
				} else if (level[i][j].equalsIgnoreCase("e")) {
					JLabel l = new JLabel("");
					l.setSize(50, 50);
					l.setOpaque(false);
					gameArea.add(l);
					labels[i][j] = l;
				}
			}
		}
		labels[posX][posY].setIcon(new ImageIcon((Toolkit.getDefaultToolkit().getImage(getRine(Integer.parseInt(Game.getInstance().getPlayerColor()), "up")).getScaledInstance(
										(this.getSize().width - 10) / 20,
										(this.getSize().height - 10) / 20,
										Image.SCALE_SMOOTH))));
	}

	public void reDraw() {
		for (int j = 0; j < 20; j++) {
			for (int i = 0; i < 20; i++) {
				if (level[i][j].equalsIgnoreCase("w")) {
					Image test = Toolkit.getDefaultToolkit().getImage("./Image/sten.png");
					labels[i][j].setIcon(new ImageIcon(test.getScaledInstance(
													(this.getSize().width - 10) / 20,
													(this.getSize().height - 10) / 20,
													Image.SCALE_SMOOTH)));
					
				}
			}
		}
	}
}
